﻿using NewChillers.Helpers;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NewChillers.Models
{
    public class AlarmParameterModel
    {
        #region 压力报警参数
        /// <summary>
        /// 高压报警值
        /// </summary>
        public double HighPressure { get; set; }

        /// <summary>
        /// 低压报警值
        /// </summary>
        public double LowPressure { get; set; }
        #endregion

        #region 温度报警参数
        /// <summary>
        /// 高温报警值
        /// </summary>
        public double HighTemperature { get; set; }

        /// <summary>
        /// 低温报警值
        /// </summary>
        public double LowTemperature { get; set; }
        #endregion

        #region 流量报警参数
        /// <summary>
        /// 高流量报警值
        /// </summary>
        public double HighFlow { get; set; }

        /// <summary>
        /// 低流量报警值
        /// </summary>
        public double LowFlow { get; set; }
        #endregion

        #region 其他参数
        /// <summary>
        /// 报警延时（秒）
        /// </summary>
        public int AlarmDelay { get; set; }
        #endregion

        public AlarmParameterModel()
        {
            LoadFromConfig();
        }

        #region 配置文件操作方法
        /// <summary>
        /// 从配置文件加载参数
        /// </summary>
        public void LoadFromConfig()
        {
            try
            {
                // 从app.config读取参数，如果不存在则使用默认值
                HighPressure = ConfigHelper.GetDouble("Alarm.HighPressure", 10.0);
                LowPressure = ConfigHelper.GetDouble("Alarm.LowPressure", 1.0);
                HighTemperature = ConfigHelper.GetDouble("Alarm.HighTemperature", 80.0);
                LowTemperature = ConfigHelper.GetDouble("Alarm.LowTemperature", 5.0);
                HighFlow = ConfigHelper.GetDouble("Alarm.HighFlow", 100.0);
                LowFlow = ConfigHelper.GetDouble("Alarm.LowFlow", 10.0);
                AlarmDelay = ConfigHelper.GetInt("Alarm.AlarmDelay", 5);
            }
            catch (Exception ex)
            {
                // 如果读取配置失败，使用默认值
                SetDefaultValues();
            }
        }

        /// <summary>
        /// 保存参数到配置文件
        /// </summary>
        public void SaveToConfig()
        {
            try
            {
                // 保存参数到app.config
                ConfigHelper.SetValue("Alarm.HighPressure", HighPressure);
                ConfigHelper.SetValue("Alarm.LowPressure", LowPressure);
                ConfigHelper.SetValue("Alarm.HighTemperature", HighTemperature);
                ConfigHelper.SetValue("Alarm.LowTemperature", LowTemperature);
                ConfigHelper.SetValue("Alarm.HighFlow", HighFlow);
                ConfigHelper.SetValue("Alarm.LowFlow", LowFlow);
                ConfigHelper.SetValue("Alarm.AlarmDelay", AlarmDelay);
            }
            catch (Exception ex)
            {
                throw new Exception("保存报警参数到配置文件失败", ex);
            }
        }

        /// <summary>
        /// 设置默认值
        /// </summary>
        private void SetDefaultValues()
        {
            HighPressure = 10.0;
            LowPressure = 1.0;
            HighTemperature = 80.0;
            LowTemperature = 5.0;
            HighFlow = 100.0;
            LowFlow = 10.0;
            AlarmDelay = 5;
        }
        #endregion
    }
}
